package com.bezdelnick.forum.web;

import com.bezdelnick.forum.repository.FileSystemFolderRepository;
import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.page.*;
import com.google.code.jqwicket.JQComponentOnBeforeRenderListener;
import com.google.code.jqwicket.JQContributionConfig;
import examples.TestJqWicketPage;
import org.apache.wicket.Page;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.apache.wicket.request.resource.JavaScriptResourceReference;

import javax.servlet.ServletContext;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 18.07.12
 * Time: 12:49
 * To change this template use File | Settings | File Templates.
 */
public class ForumApplication extends AuthenticatedWebApplication {
    private static final Logger LOGGER = LoggerFactory.getLogger(ForumApplication.class);

    private ForumService forumService;

    @Override
    public void init() {
        super.init();
        this.getComponentInstantiationListeners().add(new SpringComponentInjector(this));

        // For JqWicket library
        JQContributionConfig config = new JQContributionConfig().withDefaultJQueryUi();
//        new JavaScriptResourceReference(ForumApplication.class, "jquery.js"))
//                .withJQueryUiJs("http://localhost:8181/js/jquery/jquery-1.8.0.min.js")
//                .withJQueryUiJs("http://localhost:8181/js/jquery/jquery-ui-1.8.23.custom.min.js")
//                .withJQueryUiCss("http://localhost:8181/js/jquery/custom-theme/jquery-ui-1.8.23.custom.css");

        getComponentPreOnBeforeRenderListeners().add(new JQComponentOnBeforeRenderListener(config));

        // Mount Index page
        mount(new MountedMapperWithoutPageComponentInfo("/index", IndexPage.class));

        // Mount Show question page
        mount(new MountedMapperWithoutPageComponentInfo("/show", ShowQuestionPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/show/${id}", ShowQuestionPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/show?${id}", ShowQuestionPage.class));

        // Mount Add question page
        mount(new MountedMapperWithoutPageComponentInfo("/ask", AddQuestionPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/ask/${id}/${action}", AddQuestionPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/ask?${id}", AddQuestionPage.class));

        // Mount Add answer page
        mount(new MountedMapperWithoutPageComponentInfo("/answer", AddAnswerPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/answer/${id}", AddAnswerPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/answer?${id}", AddAnswerPage.class));

        // Mount Admin page
        mount(new MountedMapperWithoutPageComponentInfo("/moder", ModeratorPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/moder/${action}/${id}", ModeratorPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/moder?${action}&${id}", ModeratorPage.class));

        // Mount Login page
        mount(new MountedMapperWithoutPageComponentInfo("/login", LoginPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/login?${userRequest}&${exp}", LoginPage.class));

        // Mount UserRegistration page
        mount(new MountedMapperWithoutPageComponentInfo("/registration", UserRegistrationPage.class));

        // Mount ShowUser page
        mount(new MountedMapperWithoutPageComponentInfo("/user/${id}", ShowUserPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/user?${id}", ShowUserPage.class));

        // Mount AdminRegistration page
        mount(new MountedMapperWithoutPageComponentInfo("/moder-reg", ModeratorRegistrationPage.class));

        mount(new MountedMapperWithoutPageComponentInfo("/users-list", ShowUsersListPage.class));

        // Mount SuperAdmin page
        mount(new MountedMapperWithoutPageComponentInfo("/super", SuperAdminPage.class));

        // Mount upload files page
        mount(new MountedMapperWithoutPageComponentInfo("/upload", UploadFilesPage.class));

        // Test RichTextEditor page
        mount(new MountedMapperWithoutPageComponentInfo("/example/editor1", TestRichTextEditorPage.class));
        mount(new MountedMapperWithoutPageComponentInfo("/example/editor2", TestJqWicketPage.class));

        // Mount resources from file-system-folder repository
        FileSystemFolderRepository.get().mountAllResources();
    }

    @Override
    public Class<? extends Page> getHomePage() {
        try {
            ServletContext context = getServletContext();
            WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
            forumService = (ForumService) applicationContext.getBean("forumService");
        } catch (Exception ex) {
            LOGGER.error(ex.getMessage());
        }

        if (forumService.hasSuperAuthority()) {
            return SuperAdminPage.class;
        } else if (forumService.hasModeratorAuthority()) {
            return ModeratorPage.class;
        } else {
//            return IndexPage.class;
//            return UploadFilesPage.class;
            return AddQuestionPage.class;
//            return TestRichTextEditorPage.class;
//            return TestJqWicketPage.class;
        }
    }

    @Override
    protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
        return MyAuthenticatedWebSession.class;
    }

    @Override
    protected Class<? extends WebPage> getSignInPageClass() {
        return LoginPage.class;
    }


}
